require(ggplot2)
library(gridExtra)
library(scales)
library(reshape)
require(grid)
df.results <- read.csv("it_gen_election_results.csv") # Not included

df.results.perc <- data.frame(Year = df.results$Year,
                              DC = df.results$DC / (df.results$Turnout - df.results$Spoilt),
                              PSI = df.results$PSI / (df.results$Turnout - df.results$Spoilt),
                              FDP = df.results$FDP / (df.results$Turnout - df.results$Spoilt),
                              PCI = df.results$PCI / (df.results$Turnout - df.results$Spoilt),
                              PDS = df.results$PDS / (df.results$Turnout - df.results$Spoilt),
                              DS = df.results$DS / (df.results$Turnout - df.results$Spoilt),
                              FI = df.results$FI / (df.results$Turnout - df.results$Spoilt),
                              AN = df.results$AN / (df.results$Turnout - df.results$Spoilt),
                              PPI = df.results$PPI / (df.results$Turnout - df.results$Spoilt),
                              LN = df.results$LN / (df.results$Turnout - df.results$Spoilt),
                              M = df.results$M / (df.results$Turnout - df.results$Spoilt),
                              PD = df.results$PD / (df.results$Turnout - df.results$Spoilt),
                              PDL = df.results$PDL / (df.results$Turnout - df.results$Spoilt),
                              M5S = df.results$M5S / (df.results$Turnout - df.results$Spoilt))

# Remove NA
df.results.perc <- melt(df.results.perc,id="Year",variable_name="Party")
df.results.perc <- df.results.perc[complete.cases(df.results.perc),]

# Melt lines
df.results.perc.lines <- df.results.perc
newRow <- data.frame(Year=1948, Party="PSI", value=df.results.perc.lines$value[df.results.perc.lines$Party=="FDP"])
df.results.perc.lines <- rbind(df.results.perc.lines, newRow)
df.results.perc.lines <- df.results.perc.lines[-which(df.results.perc.lines$Party=="PPI"),]
df.results.perc.lines <- df.results.perc.lines[-which(df.results.perc.lines$Party=="LN"),]
df.results.perc.lines <- df.results.perc.lines[-which(df.results.perc.lines$Party=="M"),]
df.results.perc.lines <- df.results.perc.lines[-which(df.results.perc.lines$Party=="M5S"),]
df.results.perc.lines$Party <- gsub("FI|PDL", "FI-PDL", df.results.perc.lines$Party)
df.results.perc.lines$Party <- gsub("FDP|PCI|PDS|DS|PD", "FDP-PCI-PDS-DS-PD", df.results.perc.lines$Party)

plot1 <- ggplot(df.results.perc, aes(x=Year,y=value,group=Party)) + 
  geom_point(size=1) +
  labs(y="Votes") +
  scale_y_continuous(labels = scales::percent) +
  scale_x_continuous(breaks = scales::pretty_breaks(n=10)) +
  coord_cartesian(ylim=c(0.1,0.5)) +
  scale_colour_discrete(guide = FALSE) +
  theme(axis.text.x=element_blank(),
        axis.title.x=element_blank(),
        plot.title=element_blank(),
        axis.ticks.x=element_blank(),
        plot.margin = unit(c(1,0.5,0,0.5), "lines")) +
  geom_point(data=df.results.perc[c(1,19,34,49), ], pch=5, size=5, alpha=0.6) +
  geom_text(data=df.results.perc[c(1,12,19,20,29,32,34,38,42,43,44,45,47,49),], aes(label=Party),hjust=0.5, vjust=-0.5, size=3.5) +
  geom_line(data=df.results.perc.lines, aes(x=Year, y=value, colour=Party), size=1.5, alpha=0.6)

plot2 <- ggplot(df.results, aes(Year)) +
  geom_line(aes(y = Turnout / Electorate )) +
  geom_area(aes(y = Turnout / Electorate ), fill="gray") +
  labs(y="Turnout") + 
  scale_y_continuous(labels = scales::percent, breaks = scales::pretty_breaks(n=3)) +
  scale_x_continuous(breaks = scales::pretty_breaks(n=10)) +
  coord_cartesian(ylim=c(0.5,1)) + 
  theme(plot.margin = unit(c(0,0.5,1,0.5), "lines"))


gp1<- ggplot_gtable(ggplot_build(plot1))
gp2<- ggplot_gtable(ggplot_build(plot2))
maxWidth = unit.pmax(gp1$widths[2:3], gp2$widths[2:3])
gp1$widths[2:3] <- maxWidth
gp2$widths[2:3] <- maxWidth

grid.arrange(gp1,gp2,heights=c(4/5, 1/5), ncol=1, nrow=2)
  
